﻿using System;
using Birkelund.QueryStrings.Internals;
using NUnit.Framework;

namespace Birkelund.QueryStrings.Test
{
    public class QueryStringReaderTests
    {
        [Test]
        public void ReadComplexUrl()
        {
            Uri uri = new Uri("http://translate.google.com/#en|da|how%20are%20you");
            QueryStringReader reader = QueryStringReader.CreateFromUrl(uri);

            Assert.AreEqual(0, reader.Tokens.Count);
        }

        [Test]
        public void ExistsTest()
        {
            Uri uri = new Uri("http://www.test.dk/?a=3&b=abc");
            QueryStringReader reader = QueryStringReader.CreateFromUrl(uri);

            bool doesAExist = reader.Exists(Token.Get("a"));
            bool doesBExist = reader.Exists(Token.Get("b"));
            bool doesCExist = reader.Exists(Token.Get("c"));

            Assert.IsTrue(doesAExist, "a");
            Assert.IsTrue(doesBExist, "b");
            Assert.IsFalse(doesCExist, "c");
        }


        [Test]
        public void CaseSensitiveRead()
        {
            Uri uri = new Uri("http://www.test.dk/?A=myText");
            QueryStringReader reader = QueryStringReader.CreateFromUrl(uri);

            bool doesAExist = reader.Exists(Token.Get("a")); //lowercase a
            Assert.IsTrue(doesAExist, "a");

            string actual = reader.ReadStringValue(Token.Get("a"), UriEncodingMode.HandleEncoding);
            Assert.AreEqual("myText", actual);
        }

        [Test]
        public void ReadStringTest()
        {
            Uri uri = new Uri("http://www.test.dk/?a=æ&b=%c3%a6");
            QueryStringReader reader = QueryStringReader.CreateFromUrl(uri);

            // Not valid anymore
            //string val1 = reader.ReadStringValue(Token.Get("a"), UrlEncodingMode.DoNotHandleEncoding);
            //Assert.AreEqual("æ", val1, "val1");

            string val2 = reader.ReadStringValue(Token.Get("a"), UriEncodingMode.HandleEncoding);
            Assert.AreEqual("æ", val2, "val2");

            // Not valid anymore
            //string val3 = reader.ReadStringValue(Token.Get("b"), UrlEncodingMode.DoNotHandleEncoding);
            //Assert.AreEqual("=%c3%a6", val3, "val3");

            string val4 = reader.ReadStringValue(Token.Get("b"), UriEncodingMode.HandleEncoding);
            Assert.AreEqual("æ", val4, "val4");

            // Not valid anymore
            //string val5 = reader.ReadStringValue(Token.Get("c"), UrlEncodingMode.DoNotHandleEncoding);
            //Assert.IsNull("æ", val5, "val5");

            string val6 = reader.ReadStringValue(Token.Get("c"), UriEncodingMode.HandleEncoding);
            Assert.IsNull(val6, "val6");
        }

        //Token intented for test - when querystring type doesn't matter for the test purpose
        private class Token : ITokenKey
        {
            public static Token Get(string keyName)
            {
                return new Token(keyName);
            }

            private Token(string keyName)
            {
                _key = keyName;
            }

            private string _key;
            public string KeyName
            {
                get { return _key; }
            }
        }
    }
}
